package com.bingo.generate.comm.util;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * ClassName    基础帮助类
 * Description
 * Create by lc
 * Date 2021/7/9 16:41
 */
public class BaseUtil {

    public static List<Class> getAllCla(String pkgName)  {
        List<Class> list=new ArrayList<>();
        Enumeration<URL> urls = null;
        try {
            urls = ClassLoader.getSystemClassLoader().getResources(pkgName.replace(".", "/"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        while (urls.hasMoreElements()) {

            URL url = (URL) urls.nextElement();

            if ("file".equals(url.getProtocol())) {
                String realPath = url.getFile();
                File pkgFile = new File(realPath);
                if (!pkgFile.exists()) {
                    /*File[] files = file.listFiles();
                    for (File f : files) {
                        System.out.println(f.getName());

                    }*/
                    continue;
                }
                File[] files = pkgFile.listFiles();
                for (File file : files) {
                    String suffix=".class";
                    if(!file.getName().endsWith(suffix)){
                        continue;
                    }
                    //  去掉.java
                    String name=file.getName().substring(0,file.getName().length()-suffix.length());
                    String allName=pkgName+"."+name;
                    Class cla= null;
                    try {
                        cla = Class.forName(allName);
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException(e);
                    }
                    list.add(cla);
                }
            }
        }
        return list;
    }

}
